let app = getApp();

Page({
  data: {
    filterParams: {},
    url:'',
    consultants: [],       // 咨询师列表
    pageNum: 1,            // 当前页码
    pageSize: 10,          // 每页条数
    pages: 0,              // 总页数
    isLoading: false       // 加载状态
  },
// 打开筛选页
openFilter() {
  wx.navigateTo({
    url: '/pages/consult/filter/filter', 
    success: (res) => {
      res.eventChannel.on('filterData', (data) => {
        this.setData({
          filterParams: data, 
        });
        this.setData({ pageNum: 1, consultants: [] }); 
        this.getConsultants();
      });
    }
  });
},
  // 获取咨询师列表
  getConsultants() {
    this.setData({ isLoading: true });
    wx.showLoading({ title: '加载中...' });

    wx.request({
      url: app.globalData.url + 'consultant/page', 
      method: 'GET',
      data: {
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize
      },
      success: (resp) => {
        if (resp.data.code === 200 && resp.data.data) {
          this.setData({
            consultants: [...this.data.consultants, ...resp.data.data.list],
            pages: resp.data.data.pages
          });
        }
      },
      complete: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        this.setData({ isLoading: false });
      }
    });
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.setData({ pageNum: 1, consultants: [] });
    this.getConsultants();
  },

  // 上拉加载更多
  onReachBottom() {
    if (this.data.isLoading || this.data.pageNum >= this.data.pages) {
      wx.showToast({ title: '没有更多数据了', icon: 'none' });
      return;
    }
    this.setData({ pageNum: this.data.pageNum + 1 });
    this.getConsultants();
  },

  // 页面加载时初始化数据
  onLoad() {
    this.setData({
      url:app.globalData.url
    });
    this.getConsultants();
  }
});